package vn.com.tiz.bookcase;

import java.util.ArrayList;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import vn.com.tiz.DocumentZ.R;
import vn.com.tiz.DocumentZ.view.BookPageView;
import vn.com.tiz.DocumentZ.view.WipeView;
import vn.com.tiz.DocumentZ.view.WipeView.OnScreenSwitchListener;
import vn.com.tiz.bookcase.entry.ChapterInfo;
import vn.com.tiz.bookcase.task.LoadChapterTask;
import vn.com.tiz.bookcase.task.LoadContentTask;
import vn.com.tiz.core.AppSetting;
import vn.com.tiz.core.TizActivity;
import vn.com.tiz.core.task.TaskResultCallback;

public class BookViewActivity extends TizActivity{

	ArrayList<String> pages;
	ArrayList<String> chapterList;
	private WipeView mainview;
	
	int MaxLine = 32;
	private int ChapterCount = 10;
	private int PageIndex = 0;
	private int ChapterIndex= 0;
	private BookPageView begin_page;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setNoTitleBar(true);
		pages = new ArrayList<String>();
		
		mainview = new WipeView(this);
		begin_page = new BookPageView(this);
		begin_page.setBackgroundResource(R.drawable.coverbook);		
		mainview.addView(begin_page);
		
		int height = begin_page.getTextHeight();
		int maxline = (int)((1134-20)/height);
		Log.d("TrungTT","Text Height:"+height+" Screeen Height:"+AppSetting.SCREEN_HEIGHT+" max line:"+maxline);
		MaxLine = maxline-1;
		
		ArrayList<ChapterInfo> chapterList = new ArrayList<ChapterInfo>();
		for(int i = 0;i<10;i++){
			ChapterInfo chapter = new ChapterInfo();
			chapter.ChapterIndex = i+1;			
			chapter.ChapterName = this.getString(R.string.chapter_text);
			chapterList.add(chapter);
		}
		
		BookPageView index_page = new BookPageView(this);				
		index_page.setupIndexPage(chapterList);
		mainview.addView(index_page);
		
		setContentView(mainview);
		
		mainview.setOnScreenSwitchListener(new OnScreenSwitchListener() {
			
			public void onScreenSwitched(int screen) {
				// TODO Auto-generated method stub
				Log.e("TrungTT","Screen:"+(screen-1));
				int page = screen - 2;
				int max = pages.size();
				
				if(page==(max-3)){
					loadNextChapter();
				}
			}
					
		});
		
		loadNextChapter();				
		//Log.d("TrungTT","end oncreate,height of page:"+begin_page.getHeightOfPage());
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		return super.onCreateOptionsMenu(menu);
	}
	
	public void gotoChapter(int chapterIndex){
		
	}
	
	private void loadNextChapter(){
		ChapterIndex++;
		if(ChapterIndex > ChapterCount){
			Log.d("TrungTT","Has reach last chapter !");
		}else{
			
			//final LoadContentTask loadbook = new LoadContentTask(this,begin_page.txvContent.getPaint(),ChapterIndex);
			final LoadChapterTask loadbook = new LoadChapterTask(this,begin_page.txvContent.getPaint(),ChapterIndex);
			loadbook.setCallback(new TaskResultCallback<Integer>() {
				
				public void onCallback(Integer result) {
					// TODO Auto-generated method stub
					Log.d("TrungTT","Load chapter done !");
					//loadbook.
					ArrayList<String> nextpages = loadbook.getPages();
					
					if(nextpages == null){
						Log.d("TrungTT","no page !");
						return;
					}
					//Log.d("TrungTT","page count:"+pages.size());	
					for(String str:nextpages){
						//Log.d("TrungTT","add page !");
						BookPageView page = new BookPageView(BookViewActivity.this);
						page.loadContent(str);
						mainview.addView(page);
					}
					
					pages.addAll(nextpages);
				}
				
			});
			loadbook.execute();
		}
		
	}
}
